package com.common.ui.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.common.entity.NoteBean;

import java.util.ArrayList;
import java.util.List;

public class SQLiteDB {

    private static final String DATABASE_NAME = "note.db";  //数据库名字
    private static final int DATABASE_VERSION = 1;         //数据库版本号

    private static SQLiteDB mSQLiteDB;
    private static SQLiteDatabase mSQLiteDatabase;

    public SQLiteDB(Context context) {
        DataBaseHelp dataBaseHelp = new DataBaseHelp(context, DATABASE_NAME, null, DATABASE_VERSION);
        mSQLiteDatabase = dataBaseHelp.getReadableDatabase();
    }

    public static SQLiteDB getInstance(Context context) {
        if (mSQLiteDB == null) {
            mSQLiteDB = new SQLiteDB(context);
        }
        return mSQLiteDB;
    }

    public boolean addNote(NoteBean noteBean) {
        boolean flag = false;
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", noteBean.getTitle());
        contentValues.put("content", noteBean.getContent());
        contentValues.put("add_time", noteBean.getAdd_time());
        contentValues.put("update_time", noteBean.getUpdate_time());
        contentValues.put("note_code", noteBean.getNote_code());
        long insert = mSQLiteDatabase.insert("note", null, contentValues);
        if (insert >= 1) {
            flag = true;
        }
        return flag;
    }

    public boolean updateNote(NoteBean noteBean) {
        boolean flag = false;
        ContentValues contentValues = new ContentValues();
        contentValues.put("title", noteBean.getTitle());
        contentValues.put("content", noteBean.getContent());
        contentValues.put("update_time", noteBean.getUpdate_time());
        contentValues.put("note_code", noteBean.getNote_code());
        int update = mSQLiteDatabase.update("note", contentValues, "note_id=?", new String[]{noteBean.getNote_id()});
        if (update >= 1) {
            flag = true;
        }
        return flag;
    }

    public boolean deleteNote(String id) {
        boolean flag = false;
        int delete = mSQLiteDatabase.delete("note", "note_id=?", new String[]{id});
        if (delete >= 1) {
            flag = true;
        }
        return flag;
    }

    public List<NoteBean> findAllData(String orderBy) {
        List<NoteBean> noteBeans = new ArrayList<>();
        Cursor cursor = mSQLiteDatabase.query("note", new String[]{"note_id", "title", "content", "add_time", "update_time", "note_code"}
                , null, null, null, null, orderBy + ("title".equals(orderBy) ? " asc" : " desc"));
        if (cursor != null) {
            while (cursor.moveToNext()) {
                NoteBean noteBean = new NoteBean();
                noteBean.setNote_id(cursor.getString(cursor.getColumnIndex("note_id")));
                noteBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                noteBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                noteBean.setAdd_time(cursor.getString(cursor.getColumnIndex("add_time")));
                noteBean.setUpdate_time(cursor.getString(cursor.getColumnIndex("update_time")));
                noteBean.setNote_code(cursor.getString(cursor.getColumnIndex("note_code")));
                noteBeans.add(noteBean);
            }
        }
        return noteBeans;
    }

    public List<NoteBean> findDataByContent(String content, String orderBy) {
        List<NoteBean> noteBeans = new ArrayList<>();
        Cursor cursor = mSQLiteDatabase.query("note", new String[]{"note_id", "title", "content", "add_time", "update_time", "note_code"}
                , "content like '%" + content + "%' or title like '%"+content+"%'", null, null, null, orderBy + ("title".equals(orderBy) ? " asc" : " desc"));
        if (cursor != null) {
            while (cursor.moveToNext()) {
                NoteBean noteBean = new NoteBean();
                noteBean.setNote_id(cursor.getString(cursor.getColumnIndex("note_id")));
                noteBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                noteBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                noteBean.setAdd_time(cursor.getString(cursor.getColumnIndex("add_time")));
                noteBean.setUpdate_time(cursor.getString(cursor.getColumnIndex("update_time")));
                noteBean.setNote_code(cursor.getString(cursor.getColumnIndex("note_code")));
                noteBeans.add(noteBean);
            }
        }
        return noteBeans;
    }

    public NoteBean findDataByCode(String code) {
        NoteBean noteBean = new NoteBean();
        Cursor cursor = mSQLiteDatabase.rawQuery("select *  from note where note_code=?", new String[]{code});
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                noteBean.setNote_id(cursor.getString(cursor.getColumnIndex("note_id")));
                noteBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                noteBean.setContent(cursor.getString(cursor.getColumnIndex("content")));
                noteBean.setAdd_time(cursor.getString(cursor.getColumnIndex("add_time")));
                noteBean.setUpdate_time(cursor.getString(cursor.getColumnIndex("update_time")));
                noteBean.setNote_code(cursor.getString(cursor.getColumnIndex("note_code")));
            }
        }
        return noteBean;
    }


}
